package adata;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import model.ApplicationModel;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import db.DbException;
import forms.ApplicantForm;
import forms.Degree;
import forms.FileUploadForm;

public class VerificationPageAction extends Action{
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, 
		    HttpServletRequest request, HttpServletResponse response) 
		    throws DbException, Exception{
		
		ApplicantForm ad = (ApplicantForm) form;
		// Degree deg = (Degree) form;
		
		List<Degree> theDegreeArray = (LinkedList<Degree>) request.getSession().getAttribute("TheSessionDegreeArray");
			
		Integer currentCount = (Integer)(request.getSession().getAttribute("SessionCounter"));
		
		request.getSession().setAttribute ("TheSessionDegreeArray", theDegreeArray);
		
		
		List<Degree> degreeList = (LinkedList<Degree>) request.getSession().getAttribute("TheSessionDegreeArray");
		
		// insert into students table
		int appID = ApplicationModel.insertApplicant(ad);
		request.setAttribute("appID", appID);
		
		/*
		// insert custom major into disciplines table
		if (request.getSession().getAttribute("otherMajor") != null)  {
			String otherMajor = request.getSession().getAttribute("otherMajor").toString();
			ApplicationModel.insertOtherMajor(otherMajor);
		}
		else {
			// insert into student_degrees table
			ApplicationModel.insertStudentDeg(degreeList, currentCount);
		}
		*/
		
		for (int i = 0; i <= currentCount; i++)  {
			// insert into degrees table
			ApplicationModel.insertDegree(ad, currentCount, theDegreeArray);	
		}
		
		// insert into student_degrees table
		ApplicationModel.insertStudentDeg(degreeList, currentCount);
		
		// insert into student_specials table
		ApplicationModel.insertStudentSpec(ad.getSpecialization());
		
		
		// update status in users table from "notSubmitted" to "submitted"
		String currentUser = request.getUserPrincipal().getName();
		ApplicationModel.updateAppStatus(currentUser, appID);
		
		
		
		return mapping.findForward("success");
	}
}
